package com.xinai.adspter;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xinai.R;
import com.xinai.activity.SalesActivity;
import com.xinai.util.LoginParameterUtil;
import com.xinai.util.Tools;

import java.util.List;
import java.util.Map;
/*
* 这个是通用适配器，多个ac共用
* */
public class SalesAdspter extends BaseAdapter {

    private List<Map<String, Object>> data;
    private LayoutInflater layoutInflater;
    private Context context;

    public SalesAdspter(Context context, List<Map<String, Object>> data) {
        this.context = context;
        this.data = data;
        this.layoutInflater = LayoutInflater.from(context);
    }
    /**
     * 组件集合，对应list_sales_invoice.xml中的控件
     */
    public final class Zujian {
        public TextView number; // 单号
        public TextView department;
        public TextView customer;
        public TextView count;
        public TextView amount;
        public TextView invoicedate;
        public TextView employee;
        public TextView memo;
        public TextView brand;
        public TextView other;
        public LinearLayout ll_second;
        public LinearLayout ll_third;
        public LinearLayout ll_fourth;
    }
    public void refresh(List<Map<String, Object>> list) {
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    /**
     * 获得某一位置的数据
     */
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    /**
     * 获得唯一标识
     */
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Zujian zujian = null;
        if (convertView == null) {
            zujian = new Zujian();
            Activity activity = (Activity) context;
            // 获得组件，实例化组件
            convertView = layoutInflater.inflate(R.layout.list_sales_invoice, null);
            zujian.number = (TextView) convertView.findViewById(R.id.tv_number);
            zujian.customer = (TextView) convertView.findViewById(R.id.tv_customer);
            zujian.amount = (TextView) convertView.findViewById(R.id.tv_amount);
            zujian.count = (TextView) convertView.findViewById(R.id.tv_count);
            zujian.invoicedate = (TextView) convertView.findViewById(R.id.tv_invoicedate);
            zujian.employee = (TextView) convertView.findViewById(R.id.tv_employee);
            zujian.memo = (TextView) convertView.findViewById(R.id.tv_memo);
            zujian.other = (TextView) convertView.findViewById(R.id.tv_other);
            zujian.department = (TextView) convertView.findViewById(R.id.tv_department);
            zujian.brand = (TextView) convertView.findViewById(R.id.tv_brand);
            zujian.ll_second = (LinearLayout) convertView.findViewById(R.id.ll_second);
            zujian.ll_third = (LinearLayout) convertView.findViewById(R.id.ll_third);
            zujian.ll_fourth = (LinearLayout) convertView.findViewById(R.id.ll_fourth);
            // 根据系统设置的字段权限显示
            dynamicDisplay(activity, zujian);
            convertView.setTag(zujian);
        } else {
            zujian = (Zujian) convertView.getTag();
        }
        // 绑定数据
        Activity activity = (Activity) context;
        zujian.number.setText(String.valueOf(data.get(position).get("id")));
        zujian.department.setText(String.valueOf(data.get(position).get("warehouseName")));
     /*   if (activity instanceof PurchaseActivity || activity instanceof PurchaseReturnActivity) {
            zujian.customer.setText(String.valueOf(data.get(position).get("Supplier")));
        } else {
            zujian.customer.setText(String.valueOf(data.get(position).get("Customer")));
        }
        if (activity instanceof WarehouseStockInActivity || activity instanceof WarehouseStockOutActivity || activity instanceof StockMoveActivity) {
            zujian.amount.setText("￥" + Tools.formatDecimal(String.valueOf(data.get(position).get("RelationAmountSum"))));
        } else if (activity instanceof InventorySheetActivity) {
            zujian.amount.setText("￥" + Tools.formatDecimal(String.valueOf(data.get(position).get("RetailAmountSum"))));
        } else {
            zujian.amount.setText("￥" + Tools.formatDecimal(String.valueOf(data.get(position).get("AmountSum"))));
        } */

        zujian.customer.setText(String.valueOf(data.get(position).get("customName")));

        zujian.amount.setText("￥" + Tools.formatDecimal(String.valueOf(data.get(position).get("amt"))));
        zujian.count.setText(String.valueOf(data.get(position).get("qty")));
        zujian.invoicedate.setText(String.valueOf(data.get(position).get("invoiceDate")));
        zujian.employee.setText(String.valueOf(data.get(position).get("Employee")));
        zujian.brand.setText(String.valueOf(data.get(position).get("Brand")));

        zujian.memo.setVisibility(View.GONE);

        if(activity instanceof SalesActivity){

          //  zujian.other.setText(String.valueOf(data.get(position).get("OrderNo")));
        }


     /*   if (activity instanceof SalesOrderActivity) {
            zujian.other.setText(String.valueOf(data.get(position).get("Warehouse")));
        } else if (activity instanceof SalesActivity) {
            zujian.other.setText(String.valueOf(data.get(position).get("OrderNo")));
        } else if (activity instanceof SalesReturnsActivity) {
            zujian.other.setText(String.valueOf(data.get(position).get("Warehouse")));
        } else if (activity instanceof StockMoveActivity) {
            zujian.other.setText(String.valueOf(data.get(position).get("WarehouseIn")));
        } else if (activity instanceof WarehouseStockInActivity || activity instanceof WarehouseStockOutActivity) {
            zujian.other.setText(String.valueOf(data.get(position).get("RelationWarehouse")));
        }
        String memo = String.valueOf(data.get(position).get("Memo")).trim();
        if (memo != null && !memo.isEmpty() && !"".equals(memo) && memo.length() > 11) {
            memo = memo.substring(0, 11) + "...";
        }
        zujian.memo.setText(memo);
         */
        return convertView;
    }
    private void dynamicDisplay(Activity activity, Zujian zujian) {
        // 销售发货单
        if (activity instanceof SalesActivity) {
            if (!LoginParameterUtil.salesEmployee) {
                zujian.employee.setVisibility(View.GONE);
            } else {
                zujian.employee.setVisibility(View.VISIBLE);
            }
            if (!LoginParameterUtil.salesCustomer) {
                zujian.customer.setVisibility(View.GONE);
            } else {
                zujian.customer.setVisibility(View.VISIBLE);
            }
            if (!LoginParameterUtil.salesBrand) {
                zujian.brand.setVisibility(View.GONE);
            } else {
                zujian.brand.setVisibility(View.VISIBLE);
            }
            if (!LoginParameterUtil.salesMemo) {
                zujian.memo.setVisibility(View.GONE);
            } else {
                zujian.memo.setVisibility(View.VISIBLE);
            }
            if (!LoginParameterUtil.salesOrderNo) {
                zujian.other.setVisibility(View.GONE);
            } else {
                zujian.other.setVisibility(View.VISIBLE);
            }
            if (!LoginParameterUtil.salesWarehouse) {
                zujian.department.setVisibility(View.GONE);
            } else {
                zujian.department.setVisibility(View.VISIBLE);
            }
            if (!LoginParameterUtil.salesAmountSum) {
                zujian.amount.setVisibility(View.GONE);
            } else {
                zujian.amount.setVisibility(View.VISIBLE);
            }
        }
    }

}
